package com.taptech.doufu.ui.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.PopupWindow;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.adapter.NovelSortAdapter;
import com.taptech.doufu.bean.NovelSortBean;
import com.taptech.doufu.bean.NovelSortUpBean;

import java.util.List;

/**
 * Created by ${LiYi} on 2017/1/4.
 */

public class ComplexSortPopWindow extends PopupWindow implements AdapterView.OnItemClickListener,View.OnClickListener {
    private Context mContext;
    private NovelSortUpBean novelSortUpBean;
    private NoScrollGridView noScrollGridView;
    private View outLayout;
    private NovelSortAdapter sortAdapter ;
    private ComplexSelSort complexSelSort;

    public  ComplexSortPopWindow(Context mContext,NovelSortUpBean novelSortUpBean){
        super(mContext);
        this.mContext = mContext;
        this.novelSortUpBean = novelSortUpBean;
        initView();
    }

    public void setComplexSelSort(ComplexSelSort complexSelSort) {
        this.complexSelSort = complexSelSort;
    }

    private void initView(){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final  View contentView = inflater.inflate(R.layout.sort_complex_pop_layout, null);
        noScrollGridView = (NoScrollGridView) contentView.findViewById(R.id.sort_complex_gdView);
        noScrollGridView.setOnItemClickListener(this);
        outLayout =  contentView.findViewById(R.id.sort_complex_out_layout);
        //设置SelectPicPopupWindow的View
        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        outLayout.setOnClickListener(this);
    }

    public void setNovelSortUpBean(NovelSortUpBean novelSortUpBean){
        this.novelSortUpBean = novelSortUpBean;
        initDate();
    }
    private void initDate(){
        List<NovelSortBean> novelSortBeanList = null;
        if(novelSortUpBean != null ){
            novelSortBeanList = novelSortUpBean.getList();
        }
        sortAdapter = new NovelSortAdapter(mContext,novelSortBeanList);

        NovelSortBean selNovelSort = null;
        if (novelSortUpBean != null) {
            selNovelSort = novelSortUpBean.getSelNovelSortBean();
        }

        //初始化选择项
        if(selNovelSort == null && novelSortBeanList != null && novelSortBeanList.size() > 0){
            selNovelSort = novelSortBeanList.get(0);
            if (novelSortUpBean != null) {
                novelSortUpBean.setSelNovelSortBean(selNovelSort);
            }
        }

        sortAdapter.setSelNovelSortBean(selNovelSort);
        noScrollGridView.setAdapter(sortAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NovelSortBean novelSortBean = sortAdapter.getItem(position);
        if(novelSortUpBean != null){
            novelSortUpBean.setSelNovelSortBean(novelSortBean);
        }
        if(complexSelSort != null){
            complexSelSort.selSortListener(novelSortUpBean);
        }
        dismiss();
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public interface ComplexSelSort{
        public void selSortListener(NovelSortUpBean novelSortUpBean);
    }
}
